﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using MxWeiXinPF.DBUtility;//Please add references
namespace MxWeiXinPF.DAL
{
	/// <summary>
	/// 数据访问类:wxapp_buyOrders
	/// </summary>
	public partial class wxapp_buyOrders
	{
		public wxapp_buyOrders()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("id", "wxapp_buyOrders"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from wxapp_buyOrders");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(MxWeiXinPF.Model.wxapp_buyOrders model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into wxapp_buyOrders(");
			strSql.Append("userId,username,storeName,wxappId,wxappName,price,status,orderNo,tradeNo,paymentType,paymentStatus,paymentTime,createTime,createPersonId,buyType,remark)");
			strSql.Append(" values (");
			strSql.Append("@userId,@username,@storeName,@wxappId,@wxappName,@price,@status,@orderNo,@tradeNo,@paymentType,@paymentStatus,@paymentTime,@createTime,@createPersonId,@buyType,@remark)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@userId", SqlDbType.Int,4),
					new SqlParameter("@username", SqlDbType.VarChar,50),
					new SqlParameter("@storeName", SqlDbType.VarChar,100),
					new SqlParameter("@wxappId", SqlDbType.Int,4),
					new SqlParameter("@wxappName", SqlDbType.VarChar,150),
					new SqlParameter("@price", SqlDbType.Decimal,9),
					new SqlParameter("@status", SqlDbType.Int,4),
					new SqlParameter("@orderNo", SqlDbType.VarChar,100),
					new SqlParameter("@tradeNo", SqlDbType.VarChar,150),
					new SqlParameter("@paymentType", SqlDbType.VarChar,50),
					new SqlParameter("@paymentStatus", SqlDbType.Int,4),
					new SqlParameter("@paymentTime", SqlDbType.DateTime),
					new SqlParameter("@createTime", SqlDbType.DateTime),
					new SqlParameter("@createPersonId", SqlDbType.Int,4),
					new SqlParameter("@buyType", SqlDbType.Int,4),
					new SqlParameter("@remark", SqlDbType.VarChar,1000)};
			parameters[0].Value = model.userId;
			parameters[1].Value = model.username;
			parameters[2].Value = model.storeName;
			parameters[3].Value = model.wxappId;
			parameters[4].Value = model.wxappName;
			parameters[5].Value = model.price;
			parameters[6].Value = model.status;
			parameters[7].Value = model.orderNo;
			parameters[8].Value = model.tradeNo;
			parameters[9].Value = model.paymentType;
			parameters[10].Value = model.paymentStatus;
			parameters[11].Value = model.paymentTime;
			parameters[12].Value = model.createTime;
			parameters[13].Value = model.createPersonId;
			parameters[14].Value = model.buyType;
			parameters[15].Value = model.remark;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(MxWeiXinPF.Model.wxapp_buyOrders model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update wxapp_buyOrders set ");
			strSql.Append("userId=@userId,");
			strSql.Append("username=@username,");
			strSql.Append("storeName=@storeName,");
			strSql.Append("wxappId=@wxappId,");
			strSql.Append("wxappName=@wxappName,");
			strSql.Append("price=@price,");
			strSql.Append("status=@status,");
			strSql.Append("orderNo=@orderNo,");
			strSql.Append("tradeNo=@tradeNo,");
			strSql.Append("paymentType=@paymentType,");
			strSql.Append("paymentStatus=@paymentStatus,");
			strSql.Append("paymentTime=@paymentTime,");
			strSql.Append("createTime=@createTime,");
			strSql.Append("createPersonId=@createPersonId,");
			strSql.Append("buyType=@buyType,");
			strSql.Append("remark=@remark");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@userId", SqlDbType.Int,4),
					new SqlParameter("@username", SqlDbType.VarChar,50),
					new SqlParameter("@storeName", SqlDbType.VarChar,100),
					new SqlParameter("@wxappId", SqlDbType.Int,4),
					new SqlParameter("@wxappName", SqlDbType.VarChar,150),
					new SqlParameter("@price", SqlDbType.Decimal,9),
					new SqlParameter("@status", SqlDbType.Int,4),
					new SqlParameter("@orderNo", SqlDbType.VarChar,100),
					new SqlParameter("@tradeNo", SqlDbType.VarChar,150),
					new SqlParameter("@paymentType", SqlDbType.VarChar,50),
					new SqlParameter("@paymentStatus", SqlDbType.Int,4),
					new SqlParameter("@paymentTime", SqlDbType.DateTime),
					new SqlParameter("@createTime", SqlDbType.DateTime),
					new SqlParameter("@createPersonId", SqlDbType.Int,4),
					new SqlParameter("@buyType", SqlDbType.Int,4),
					new SqlParameter("@remark", SqlDbType.VarChar,1000),
					new SqlParameter("@id", SqlDbType.Int,4)};
			parameters[0].Value = model.userId;
			parameters[1].Value = model.username;
			parameters[2].Value = model.storeName;
			parameters[3].Value = model.wxappId;
			parameters[4].Value = model.wxappName;
			parameters[5].Value = model.price;
			parameters[6].Value = model.status;
			parameters[7].Value = model.orderNo;
			parameters[8].Value = model.tradeNo;
			parameters[9].Value = model.paymentType;
			parameters[10].Value = model.paymentStatus;
			parameters[11].Value = model.paymentTime;
			parameters[12].Value = model.createTime;
			parameters[13].Value = model.createPersonId;
			parameters[14].Value = model.buyType;
			parameters[15].Value = model.remark;
			parameters[16].Value = model.id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from wxapp_buyOrders ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from wxapp_buyOrders ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public MxWeiXinPF.Model.wxapp_buyOrders GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 id,userId,username,storeName,wxappId,wxappName,price,status,orderNo,tradeNo,paymentType,paymentStatus,paymentTime,createTime,createPersonId,buyType,remark from wxapp_buyOrders ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			MxWeiXinPF.Model.wxapp_buyOrders model=new MxWeiXinPF.Model.wxapp_buyOrders();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public MxWeiXinPF.Model.wxapp_buyOrders DataRowToModel(DataRow row)
		{
			MxWeiXinPF.Model.wxapp_buyOrders model=new MxWeiXinPF.Model.wxapp_buyOrders();
			if (row != null)
			{
				if(row["id"]!=null && row["id"].ToString()!="")
				{
					model.id=int.Parse(row["id"].ToString());
				}
				if(row["userId"]!=null && row["userId"].ToString()!="")
				{
					model.userId=int.Parse(row["userId"].ToString());
				}
				if(row["username"]!=null)
				{
					model.username=row["username"].ToString();
				}
				if(row["storeName"]!=null)
				{
					model.storeName=row["storeName"].ToString();
				}
				if(row["wxappId"]!=null && row["wxappId"].ToString()!="")
				{
					model.wxappId=int.Parse(row["wxappId"].ToString());
				}
				if(row["wxappName"]!=null)
				{
					model.wxappName=row["wxappName"].ToString();
				}
				if(row["price"]!=null && row["price"].ToString()!="")
				{
					model.price=decimal.Parse(row["price"].ToString());
				}
				if(row["status"]!=null && row["status"].ToString()!="")
				{
					model.status=int.Parse(row["status"].ToString());
				}
				if(row["orderNo"]!=null)
				{
					model.orderNo=row["orderNo"].ToString();
				}
				if(row["tradeNo"]!=null)
				{
					model.tradeNo=row["tradeNo"].ToString();
				}
				if(row["paymentType"]!=null)
				{
					model.paymentType=row["paymentType"].ToString();
				}
				if(row["paymentStatus"]!=null && row["paymentStatus"].ToString()!="")
				{
					model.paymentStatus=int.Parse(row["paymentStatus"].ToString());
				}
				if(row["paymentTime"]!=null && row["paymentTime"].ToString()!="")
				{
					model.paymentTime=DateTime.Parse(row["paymentTime"].ToString());
				}
				if(row["createTime"]!=null && row["createTime"].ToString()!="")
				{
					model.createTime=DateTime.Parse(row["createTime"].ToString());
				}
				if(row["createPersonId"]!=null && row["createPersonId"].ToString()!="")
				{
					model.createPersonId=int.Parse(row["createPersonId"].ToString());
				}
				if(row["buyType"]!=null && row["buyType"].ToString()!="")
				{
					model.buyType=int.Parse(row["buyType"].ToString());
				}
				if(row["remark"]!=null)
				{
					model.remark=row["remark"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,userId,username,storeName,wxappId,wxappName,price,status,orderNo,tradeNo,paymentType,paymentStatus,paymentTime,createTime,createPersonId,buyType,remark ");
			strSql.Append(" FROM wxapp_buyOrders ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" id,userId,username,storeName,wxappId,wxappName,price,status,orderNo,tradeNo,paymentType,paymentStatus,paymentTime,createTime,createPersonId,buyType,remark ");
			strSql.Append(" FROM wxapp_buyOrders ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM wxapp_buyOrders ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from wxapp_buyOrders T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "wxapp_buyOrders";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

